package com.ls.lserp.modules.setting.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.modules.setting.dao.OgnizationDao;
import com.ls.lserp.modules.setting.entity.Ognization;
import com.ls.lserp.modules.sys.entity.Dict;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.DictUtils;
import com.ls.lserp.modules.sys.utils.SequenceUtil;

/**
 * @ClassName: FabricatingService
 * @Description: 加工厂信息数据保存 更新 删除
 * 
 * @author 
 * @date 2017年1月9日 下午18:40:40
 *
 */

@Service
@Transactional(readOnly = true)
public class FabricatingService extends CrudService<OgnizationDao, Ognization>{
	
    @Autowired
    private OgnizationDao ognizationDao;
    
    /**
      * @Title: findList
      * @Description: 查询
      * @param @param page
      * @param @param ognization
      * @param @return
      * @return Page<ognization> 
      * @throws
      */
    public Page<Ognization> findList(Page<Ognization> page, Ognization ognization) {
        // 设置分页参数
    	ognization.setPage(page);
        // 执行分页查询
        page.setList(ognizationDao.findList(ognization));
        
        return page;
    }
    
    /**
     * @Title: getOgnizationData
     * @Description: 查询
     * @param @param id
     * @param @return
     * @return Ognization
     * @throws
     */
   public Ognization getOgnizationData(Long id) {
   
	   return ognizationDao.getOgnizationData(id);
   }
   
	/**
	  * @Title: saveFabricating
	  * @Description: 保存数据
	  * @param @param ognization
	  * @return void 
	  * @throws
	  */
	@Transactional(readOnly = false)
	public void saveFabricating(Ognization ognization) {
		
		// 判断新建编辑
		//若为null 执行插入语句（新建）
		if (ognization.getId()==null){
			
			//国家取得
            Dict dictCountry=DictUtils.getDictEntity(ognization.getOgnizationCountry());
			//城市取得
            Dict dictCity=DictUtils.getDictEntity(ognization.getOgnizationCity());
			//客户类别取得
            Dict dictCategory=DictUtils.getDictEntity(ognization.getOgnizationCategoryCd());
			//客户级别取得
            Dict dictLevel=DictUtils.getDictEntity(ognization.getOgnizationLevelCd());
            
            StringBuilder conCode = new StringBuilder();
            conCode.append(dictCountry.getItemShortNameEn());
            conCode.append(dictCity.getItemShortNameEn());
            conCode.append(dictCategory.getItemShortNameEn());
            conCode.append(DateUtils.formatDate(new Date(), "yyyy"));
            conCode.append(dictLevel.getItemShortNameEn());
            // 店铺编号
            Sequence sequence=SequenceUtil.ognizationCode(conCode.toString(), "sequence_fabricating", 4);
            ognization.setOgnizationCode(sequence.getReturnCode());
            
			ognization.preInsert();
			ognizationDao.insert(ognization);
			Long ognId = ognization.getId();
			Ognization companyInfo = ognizationDao.getCompanyId(ognization.getCreateBy().getId());
			ognization.setCompanyId(companyInfo.getCompanyId());
			ognizationDao.insertOgnR(ognization);
			ognization.setId(ognId);
			ognizationDao.insertBankAccount(ognization);
			
		// 编辑
		}else{
			// 更新数据
			ognization.preUpdate();
			ognizationDao.update(ognization);
			ognizationDao.updateBankAccount(ognization);
		}
	}
	
	/**
	   * 逻辑删除
	   * @throws
	   */
	@Transactional(readOnly = false)
		 public void delete(Ognization ognization) {
		 
			 ognizationDao.delete(ognization);
		 }
}
